<?php

namespace redis;

/**
 * Class Redis
 * @package redis
 */
class Redis
{
    /**
     * redis对象
     * @var null
     */
    private static $redis = null;

    /**
     * Redis constructor.
     */
    private function __construct(){
        self::instance();
    }

    /**
     * clone
     */
    private function __clone(){}

    /**
     * 获取redis对象
     * @return null|\Redis
     */
    public static function instance()
    {
        $redis_config = config('redis');
        if(!self::$redis || !(self::$redis instanceof \Redis)){
            self::$redis = new \Redis();
            self::$redis->connect($redis_config['host'],$redis_config['port']);
            //密码
            $password = $redis_config['password'];
            if('' !== $password){
                self::$redis->auth($password);
            }
        }
        return self::$redis;
    }

    /**
     * 根据key获取指定数据
     * @param $index
     * @param $key
     * @param $value
     * @return array|mixed
     */
    public static function redis_getOrderInfoByKey($index , $key , $value)
    {
        $result = [];
        $res = json_decode(self::$redis->rPop($index) , true);
        if($res['order'][$key] == $value)
        {
            $result = $res;
        }
        else
        {
            self::$redis->lpush($index , json_encode($res));
            $result = self::redis_getOrderInfoByKey($index , $key , $value);
        }
        return $result;
    }

    /**
     * @param $key
     * @param $value
     */
    public static function redis_lpush($key, $value)
    {
        self::$redis->lPush($key , json_encode($value));
    }

    /**
     * @param $key
     * @return array
     */
    public static function redis_getAllByKey($key)
    {
        $info = self::$redis->lRange($key , 0 , -1);
        foreach ($info as $key => $value)
        {
            $info[$key] = json_decode($value , true);
        }
        return $info;
    }

    /**
     * @param $index
     * @param $key
     * @param $value
     * @return array
     */
    public static function redis_getFindByKey($index , $key , $value)
    {
        $info = self::$redis->lRange($index , 0 , -1);
        foreach ($info as $k => $val) {
            $val = json_decode($val , true);
            if ($val['order'][$key] == $value)
            {
                return $val;
            }
        }
        return [];
    }

    /**
     * @param $index
     * @param $key
     * @param $value
     * @param $fields
     * @param $val
     * @return int
     */
    public static function redis_saveListByKey($index , $key , $value , $arr , $fix = 'order')
    {
        $res = self::$redis->lRange($index , 0 , -1);
        if($res)
        {
            foreach ($res as $k => $val)
            {
                $val = json_decode($val,true);
                if($val['order'][$key] == $value)
                {
                    self::$redis->lRem($index , json_encode($val) , 1);
                    $val[$fix] = array_merge($val[$fix] , $arr);
                    self::$redis->lPush($index , json_encode($val));
                    return 1;
                }
            }
        }
        return 0;
    }



    /**
     * @param $index
     * @param $key
     * @param $value
     * @param $arr
     * @return int
     */
    public static function redis_savekey($index , $key , $value , $arr)
    {
        $res = self::$redis->lRange($index , 0 , -1);
        if($res)
        {
            foreach ($res as $k => $val)
            {
                $val = json_decode($val,true);
                if($val['order'][$key] == $value)
                {
                    self::$redis->lRem($index , json_encode($val) , 1);
                    $val = array_merge($val , $arr);
                    self::$redis->lPush($index , json_encode($val));
                    return 1;
                }
            }
        }
        return 0;
    }

    /**
     * @param $index
     * @param $key
     * @param $value
     * @param $fix
     * @return int
     */
    public static function redis_delListFixByIndex($index , $key , $value , $fix)
    {
        $res = self::$redis->lRange($index , 0 , -1);
        if($res)
        {
            foreach ($res as $k => $val)
            {
                $val = json_decode($val,true);
                if($val['order'][$key] == $value)
                {
                    self::$redis->lRem($index , json_encode($val) , 1);
                    unset($val[$fix]);
                    self::$redis->lPush($index , json_encode($val));
                    return 1;
                }
            }
        }
        return 0;
    }
}